import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:get/get.dart';
import 'package:signature/signature.dart';

import '../sign/sign_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/12/28 15:48
/// filePath： lib/page/farm/sign/sign_view.dart
/// desc: 电子签名
/// <summary>
///
class SignPage extends StatefulWidget {
  int? skuId;
  int? orderId;
  SignPage({this.skuId,this.orderId});
  @override
  _SignPageState createState() => _SignPageState();
}

class _SignPageState extends State<SignPage> {
  final logic = Get.put(SignLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.signatureController.addListener(logic.listener);
    logic.getData(3);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SignLogic>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: appBarWidget(title: "电子签名"),
          body: Padding(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  decoration: BoxDecoration(
                    border: Border(left: BorderSide(color: Colors.green,width: 2),),
                  ),
                  child: InkWidget.text(" 购买合同",color: Colors.green,fonSize: 18),
                ),
                InkWidget.text("请认真阅读并签写真实姓名（若不是后果自负）\n",color: Colors.grey),
                Expanded(
                  child: Scrollbar(
                    isAlwaysShown: true,
                    child: ListView(
                      shrinkWrap: true,
                      children: [
                        InkWidget.text("${logic.signatureData!.contractContent ?? '无合同'}",maxLines: 99999999,color: Colors.grey),
                      ],
                    ),
                  ),
                ),
                InkWidget.title("\n签写真实姓名\n"),
                Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.grey.shade200,
                          blurRadius: 3,
                          spreadRadius: 2.5,
                        ),
                      ]
                  ),
                  padding: EdgeInsets.all(10),
                  child: Stack(
                    children: [
                      Signature(
                        controller: logic.signatureController,
                        height: Get.width,
                        width: Get.width - 50,
                        backgroundColor: Colors.white,
                      ),
                      Positioned(
                        right: 0,
                        bottom: 0,
                        child: !logic.tmpIsEmpty ? SizedBox() :
                        Row(
                          children: [
                            MaterialButton(
                              onPressed: (){
                                logic.cleanSign();
                              },
                              child: Text("清除手写"),
                              color: Colors.grey[200],
                              elevation: 0.0,
                            ),
                            SizedBox(width: 10,),
                            MaterialButton(
                              onPressed: (){
                                logic.exImg();
                              },
                              child: Text(logic.path == ""? "完成手写" : "已完成手写，重写请清除",style: TextStyle(color: logic.path == "" ?Colors.green : Colors.white,),),
                              color: logic.path != "" ?Colors.green : Colors.grey[200],
                              elevation: 0.0,
                            ),
                          ],
                        ) ,
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 30,
                ),
                Center(
                  child: SizedBox(
                    width: Get.width * 0.8,
                    child: gradientButton("提 交",onTap: () async{
                      logic.setSignature(widget.orderId,);
                    },circular: 40,
                    ),
                  ),
                ),
              ],
            ),
            padding: EdgeInsets.all(15,),
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<SignLogic>();
    logic.signatureController.removeListener(logic.listener);
    logic.signatureController.dispose();
    super.dispose();
  }
}